package com.qys.livesMall.product.controller.app;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.product.entity.H2ProductCategory;
import com.qys.livesMall.product.entity.query.RequestData;
import com.qys.livesMall.product.service.H2ProductCategoryService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  商品分类
 * @author gmx
 * @since 2023-05-17
 */
@RestController
@RequestMapping("/app/h2-shop-category")
public class AppH2ProductCategoryController {
    @Resource
    private H2ProductCategoryService h2ProductCategoryService;

    /****
     * 根据父ID查询子分类,默认0
     */
    @GetMapping(value = "/parent/{pid}")
    public R<List<H2ProductCategory>> list(@PathVariable(value = "pid")Long pid){
        List<H2ProductCategory> categories = h2ProductCategoryService.queryByParentId(pid);
        return R.ok(categories);
    }
    @GetMapping(value = "/{id}")
    public R<H2ProductCategory> get(@PathVariable(value = "id")Long id){
        H2ProductCategory categories = h2ProductCategoryService.getById(id);
        return R.ok(categories);
    }

    @GetMapping(value = "/tree")
    public R<List<H2ProductCategory>> tree(){
        LambdaQueryWrapper<H2ProductCategory> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        categoryLambdaQueryWrapper.eq(H2ProductCategory::getEnable,1);
        categoryLambdaQueryWrapper.eq(H2ProductCategory::getIsDelete,0);
        List<H2ProductCategory> categories = h2ProductCategoryService.list(categoryLambdaQueryWrapper);
        return R.ok(categories);
    }
    /****
     * 分页查询
     */
    @PostMapping(value = "/pageList")
    public R<IPage<H2ProductCategory>> List(@RequestBody RequestData<H2ProductCategory> data){
        QueryWrapper<H2ProductCategory> queryWrapper = new QueryWrapper<>();
        H2ProductCategory r = data.getData();
        queryWrapper.lambda().eq(ObjectUtils.isNotNull(r.getParentId()), H2ProductCategory::getParentId, r.getParentId())
                .eq(ObjectUtils.isNotNull(r.getDisplayName()), H2ProductCategory::getDisplayName,r.getDisplayName())
                .eq(ObjectUtils.isNotNull(r.getEnable()), H2ProductCategory::getEnable, r.getEnable())
                .eq(ObjectUtils.isNotNull(r.getLevel()), H2ProductCategory::getLevel, r.getLevel())
                .eq(ObjectUtils.isNotNull(r.getName()), H2ProductCategory::getName, r.getName())
                .eq(H2ProductCategory::getEnable,1)
                .eq(H2ProductCategory::getIsDelete,0);

        Page<H2ProductCategory> res = h2ProductCategoryService.page(data.getPage(),queryWrapper);
        return R.ok(res);
    }

}
